package com.zs4sdk.entity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.common.library.util.Helper;

/**
 * 
 * function: 更新菜单每项返回json类
 * 
 * @ author: aXiBa
 */
public class MenuItemJSONResultEntity extends BaseJSONEntity {

	private static final String COLUMN_ID = "id";
	private static final String COLUMN_TITLE = "title";
	private static final String COLUMN_SEQ = "seq";
	private static final String COLUMN_URL = "url";
	
	private String id;
	private String title;
	private String seq;
	private String url;
	
//	private String tag;
	private String img_src;
	
	/**
	 * 构造方法
	 */
	public MenuItemJSONResultEntity(){
		
	}
	/**
	 * 构造方法
	 * @param json
	 */
	public MenuItemJSONResultEntity(JSONObject json){
		this.fromJSON(json);
	}
	/**
	 * 构造方法
	 * @param jsonStr
	 */
	public MenuItemJSONResultEntity(String jsonStr){
		try{
			this.fromJSON(new JSONObject(jsonStr));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public MenuItemJSONResultEntity(MenuItemJSONResultEntity entity){

		this.title = entity.getTitle();
		this.url = entity.getUrl();
		this.seq = entity.getSeq();
		this.id = entity.getId();
	}

	
	@Override
	public JSONObject toJSON() {
		JSONObject result = new JSONObject();
		try {
			result.put(COLUMN_TITLE, this.title);
			result.put(COLUMN_URL, this.url);
			result.put(COLUMN_SEQ, this.seq);
			result.put(COLUMN_ID, this.id);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public void fromJSON(JSONObject json) {
		if (Helper.isNotEmpty(json)){
			this.title = json.optString(COLUMN_TITLE);
			this.url = json.optString(COLUMN_URL);
			this.id = json.optString(COLUMN_ID);
			this.seq = json.optString(COLUMN_SEQ);
		}
	}

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getSeq() {
		return seq;
	}
	public void setSeq(String seq) {
		this.seq = seq;
	}
	
	
//	public String getTag() {
//		return tag;
//	}
//	public void setTag(String tag) {
//		this.tag = tag;
//	}
	public String getImg_src() {
		return img_src;
	}
	public void setImg_src(String img_src) {
		this.img_src = img_src;
	}
	/**
	 * 
	 * function: 由jsonArray字符串解析菜单数据
	 *
	 * @param jsonArrayStr
	 * @return
	 * 
	 * @ author: aXiBa 2013-3-28 上午10:43:08
	 */
	public static ArrayList<MenuItemJSONResultEntity> getMenuList(String jsonArrayStr) {
		
		ArrayList<MenuItemJSONResultEntity> menuEntityList = new ArrayList<MenuItemJSONResultEntity>();
		
		try {
			JSONArray array = new JSONArray(jsonArrayStr);
			if(Helper.isNotEmpty(array)) {
				for (int i = 0; i < array.length(); i++) {
					menuEntityList.add(new MenuItemJSONResultEntity(array.getJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return menuEntityList;
	}
	
	@Override
	public String toString() {
		return toJSON().toString();
	}
}
